package com.t168.appinfo.controller.background;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.t168.appinfo.controller.reception.AppInfoController;
import com.t168.appinfo.entity.AppCategory;
import com.t168.appinfo.entity.AppInfo;
import com.t168.appinfo.entity.AppVersion;
import com.t168.appinfo.entity.Dictionary;
import com.t168.appinfo.entity.PageSupport;
import com.t168.appinfo.service.background.appinfo.BgAppInfoService;
import com.t168.appinfo.service.background.appversion.BgAppVersionService;
import com.t168.appinfo.service.reception.Dictionary.DictionaryService;
import com.t168.appinfo.service.reception.appcategory.AppCategoryService;


@Controller
@RequestMapping("/bgAppInfo")
public class BgAppInfoController {

	@Resource(name = "bgAppInfoService")
	private BgAppInfoService bgAppInfoService;

	public BgAppInfoService getAppInfoService() {
		return bgAppInfoService;
	}

	public void setAppInfoService(BgAppInfoService bgAppInfoService) {
		this.bgAppInfoService = bgAppInfoService;
	}
	
	
	
	
	
	@Resource(name="dictionaryService")
	private DictionaryService dictionaryService;
	
	public DictionaryService getDictionaryService() {
		return dictionaryService;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}
	
	@Resource(name = "appCategoryService")
	private AppCategoryService appCategoryService;

	public AppCategoryService getAppCategoryService() {
		return appCategoryService;
	}

	public void setAppCategoryService(AppCategoryService appCategoryService) {
		this.appCategoryService = appCategoryService;
	}
	
	@Resource(name = "bgAppVersionService")
	public BgAppVersionService bgAppVersionService;

	public BgAppVersionService getBgAppVersionService() {
		return bgAppVersionService;
	}

	public void setBgAppVersionService(BgAppVersionService bgAppVersionService) {
		this.bgAppVersionService = bgAppVersionService;
	}
	

	@RequestMapping(value = "/appList.html")
	public String appInfo(
			@RequestParam(value="queryCategoryLevel1",required = false) String level1,
			@RequestParam(value="queryCategoryLevel2",required = false) String level2,
			@RequestParam(value="queryCategoryLevel3",required = false) String level3,
			@RequestParam(value="querySoftwareName",required = false) String softwareName,
			@RequestParam(value="queryFlatformId",required = false) String flatformId,
			@RequestParam(value="queryStatus",required = false) String state,
			@RequestParam(value="pageIndex",required = false) String pageIndex,
			HttpServletRequest request){
		//创建页面类对象
		PageSupport pageSupport = new PageSupport();
		
		if(null == pageIndex || "".equals(pageIndex)){
			pageIndex = "1";
		}
		
		int pageCount = 5;
		pageSupport.setPageSize(pageCount);
		int totalCount = bgAppInfoService.getAppInfoCount(level1, level2, level3, softwareName, flatformId,state);
		pageSupport.setTotalCount(totalCount);
		if(Integer.valueOf(pageIndex).intValue() > pageSupport.getTotalPageCount()){
			pageIndex = "1";
		}
		
		List<Dictionary> flatFromList = dictionaryService.getFlatFrom();
		//List<Dictionary> status = dictionaryService.getStatus();
		List<AppCategory> appcategorys = appCategoryService.getLevelOneCgInfo();

		if(level1 != null && !"".equals(level1)){
			List<AppCategory> levelTwos = appCategoryService.getLevelTwoCgInfo(level1);
			System.out.println("levelTwos.size()"+levelTwos.size());
			request.setAttribute("levelTwos", levelTwos);
		}
		
		if(level2 != null &&! "".equals(level2)){
			List<AppCategory> levelThrees = appCategoryService.getLevelThreeCgInfo(level2);
			System.out.println("levelThrees.size()"+levelThrees.size());
			request.setAttribute("levelThrees", levelThrees);
		}
		List<AppInfo> appInfoList = bgAppInfoService.getAppInfo(level1, level2, level3, softwareName,  flatformId,state,Integer.valueOf(pageIndex).intValue(),pageCount );
		
		request.setAttribute("querySoftwareName", softwareName);
		request.setAttribute("flatFromList", flatFromList);
		//request.setAttribute("status", status);
		request.setAttribute("appcategorys", appcategorys);
		request.setAttribute("appInfoList", appInfoList);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("totalPageCount", pageSupport.getTotalPageCount());
		request.setAttribute("totalCount", totalCount);
		request.setAttribute("totalPageCount", pageSupport.getTotalPageCount());
		request.setAttribute("softwareName", softwareName);
		request.setAttribute("level1", level1);
		request.setAttribute("level2", level2);
		request.setAttribute("level3", level3);
		request.setAttribute("flatformId", flatformId);
		request.setAttribute("state", state);
		return "jsp/backend/applist";
	}
	
	
	@RequestMapping(value = "/appCheck.html")
	public String appCheck(@RequestParam("id")Integer id,HttpServletRequest request){
		AppInfo appInfo = bgAppInfoService.getAppInfoViewById(id);
		AppVersion appVersion = bgAppVersionService.getAppInfoNewVersion(id);
		request.setAttribute("appInfo", appInfo);
		request.setAttribute("appVersion", appVersion);
		return "jsp/backend/appcheck";
	}
	
	@RequestMapping("/modifyAppInfoStatus")
	public String modifyAppInfoStatus(Integer status,Integer id,HttpServletRequest request){
		
		if(bgAppInfoService.modifyAppInfoStatus(status,id)){
			return "redirect:/bgAppInfo/appList.html";
		}
		request.setAttribute("modifyError", "审核失败");
		return "redirect:/bgAppInfo/appCheck.html?id="+id;
	}
			
	
	
	
	
}
